﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Aspfm.Main
{
    public class AspfmRightModel
    {
        public List<AspfmRightAction> Get=new List<AspfmRightAction>();
        public List<AspfmRightAction> Actions=new List<AspfmRightAction>();
        public List<AspfmRightAction> UrlAction=new List<AspfmRightAction>();
        public Hashtable paths=new Hashtable();
    }
    public class AspfmRightAction
    {
        public string dll="";
        public string clss="";
        public string cmd="";
        public string SysRoleLevel="";
        public string url="";
    }

    public class AspfmRight
    {
        public static AspfmRightModel XmlReaderAspfmRight()
        {
            AspfmRightModel AspfmRightAll = null;
            string xmlfile = BaseAshx.CurrentPath(false) + "\\bin\\AspfmRight.xml";
            if (System.IO.File.Exists(xmlfile))
            {
                object aspfmRightobj = CacheHelper.GetCache("Aspfm.AspfmRight.xml");
                if (aspfmRightobj != null && aspfmRightobj is AspfmRightModel)
                {
                    AspfmRightAll = (AspfmRightModel)aspfmRightobj;
                }
                else
                {
                    AspfmRightAll = new AspfmRightModel();
                    XmlDocument doc = new XmlDocument();
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreComments = true;//忽略文档里面的注释
                    using (XmlReader reader = XmlReader.Create(xmlfile, settings))
                    {
                        doc.Load(reader);
                        //doc.Load(@"d:/demo.xml");
                        //然后可以通过调用SelectSingleNode得到指定的结点,通过GetAttribute得到具体的属性值.参看下面的代码
                        // 得到根节点bookstore
                        XmlNode xn = doc.SelectSingleNode("Aspfm");
                        // 得到根节点的所有子节点
                        XmlNodeList xnl = xn.ChildNodes;

                        foreach (XmlNode xn1 in xnl)
                        {
                            XmlElement xe = (XmlElement)xn1;
                            if (xe.Name == "Get")
                            {
                                XmlNodeList xnl0 = xe.ChildNodes;
                                foreach (XmlNode xnl01 in xnl0)
                                {
                                    XmlElement xnl011 = (XmlElement)xnl01;
                                    AspfmRightAction action = new AspfmRightAction();
                                    action.dll = xnl011.GetAttribute("dll");
                                    action.clss = xnl011.GetAttribute("class");
                                    action.cmd = xnl011.GetAttribute("cmd");
                                    AspfmRightAll.Get.Add(action);
                                }
                            }
                            if (xe.Name == "Origin")
                            {
                                XmlNodeList xnl0 = xe.ChildNodes;
                                foreach (XmlNode xnl01 in xnl0)
                                {
                                    XmlElement xnl011 = (XmlElement)xnl01;
                                    XmlNodeList xnl02 = xnl011.ChildNodes;
                                    foreach (XmlNode xnl03 in xnl02)
                                    {
                                        XmlElement xnl031 = (XmlElement)xnl03;
                                        AspfmRightAction action = new AspfmRightAction();
                                        action.dll = xnl031.GetAttribute("dll");
                                        action.clss = xnl031.GetAttribute("class");
                                        action.cmd = xnl031.GetAttribute("cmd");
                                        action.url = xnl011.GetAttribute("value");
                                        AspfmRightAll.UrlAction.Add(action);
                                    }
                                }
                            }
                            if (xe.Name == "actions")
                            {
                                XmlNodeList xnl0 = xe.ChildNodes;
                                foreach (XmlNode xnl01 in xnl0)
                                {
                                    XmlElement xnl011 = (XmlElement)xnl01;
                                    AspfmRightAction action = new AspfmRightAction();
                                    action.dll = xnl011.GetAttribute("dll");
                                    action.clss = xnl011.GetAttribute("class");
                                    action.cmd = xnl011.GetAttribute("cmd");
                                    action.SysRoleLevel = xnl011.GetAttribute("SysRoleLevel");
                                    AspfmRightAll.Actions.Add(action);
                                }
                            }
                            if (xe.Name == "paths")
                            {
                                XmlNodeList xnl0 = xe.ChildNodes;
                                foreach (XmlNode xnl01 in xnl0)
                                {
                                    XmlElement xnl011 = (XmlElement)xnl01;
                                    AspfmRightAction action = new AspfmRightAction();
                                    AspfmRightAll.paths.Add(xnl011.GetAttribute("directory"), xnl011.GetAttribute("SysRoleLevel"));
                                }
                            }
                        }
                    }
                    CacheHelper.SetCache("Aspfm.AspfmRight.xml", AspfmRightAll, xmlfile);
                }
            }
            return AspfmRightAll;
        }
    }
}
